//
//  WmcAuthManager.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/29.
//

import Foundation
import NSObject_Rx
import RxSwift

/*
 1. 团队首页 ，成员 管理员均可查看所有人照片，管理员可删除所有人的照片，成员只能删除自己上传的照片
 */

class WmcAuthManager {
    let bag = DisposeBag()
    /// 团队相册
    var teamAblum: WmcMediaTeamAlbumModel?

    static let share = WmcAuthManager()

    private init() {}

    func canDelete(media: WmcMedia, succeed: ((_ canDelete: Bool, _ note: String?) -> Void)?) {
        guard let succeed = succeed else {
            return
        }
        getSelfMember { selfMember in

            if selfMember.memberId == media.memberId {
                // 是自己可以随便删
                succeed(true, nil)
            } else {
                // 是不是自己，只有管理员身份才可删除
                if selfMember.role == .manager {
                    succeed(true, nil)
                } else {
                    succeed(false, "非本人照片，无法删除")
                }
            }
        }
    }

    /// 获取自己作为成员的模型
    /// - Returns: description
    func getSelfMember(succeed: ((WmcTeamMemberModel) -> Void)?) {
        guard let teamId = teamAblum?.teamId else {
            return
        }
        WmcMediaTeamManagerAPI.fetchMemberList(id: teamId).subscribe { list in
            list.forEach { member in
                if member.isSelf() {
                    guard let succeed = succeed else {
                        return
                    }
                    succeed(member)
                }
            }
        } onError: { error in
            WmcHudManager.toast(error)
        }.disposed(by: bag)
    }
}
